﻿/**
* ggNetStream class extends NetStream (Actionscript 3)
* Intercepts the video stream and records important metrics before returning control back to the NetStream.
*
* Copyright (c) 2008 Glanceguide, Inc.  All rights reserved. 
*    http://glanceguide.com
* .  Decompiling, reverse engineering, copying or unauthorized redistribution is prohibited.    
**/

package com.nielsen{
	import flash.net.NetStream;
	import flash.net.NetConnection;
	import flash.events.NetStatusEvent;
	import flash.utils.setInterval;
	import flash.media.SoundTransform;

	public class ggNetStream extends NetStream {
		public var copyright:String="Copyright (c) 2008 Glanceguide, Inc";
		private var _ggClient:ggClient;
		private var _flvName:String;
		private var _videoInfo:String;
		public function ggNetStream( nc:NetConnection ) {
			this.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
			setInterval( function( stream:Object ){ ggCom.getInstance().PM( "E49", stream.time ); }, 2000, this );
			super( nc );
		}
		public function set ggSoundTransform( value:Object ):void {
			this.soundTransform=value.getSoundTransform();
		}
		// videoinfo for the subsequent Play call; title, vidtype etc.
		public function setVideoInfo(vi:String) {
			_videoInfo=vi;
		}


		private function netStatusHandler( event:NetStatusEvent ):void {
			ggCom.getInstance().PM( "onStatus", event.info.code, this.time );
		}
		public override function play( ... arguments ):void {
			if (arguments[0]!=null) {
				_flvName=arguments[0];
			}

			if (_ggClient==null||_ggClient!=this.client) {
				this.client=_ggClient=new ggClient(this.client,_flvName,_videoInfo,this);
			}

			ggCom.getInstance().PM( "E4", _flvName );
			ggCom.getInstance().PM( "E5", 0 );

			super.play( arguments[0] );
		}
		public override function pause( ):void {
			ggCom.getInstance().PM( "E6", this.time );

			super.pause( );
		}
		public override function seek( offset:Number ):void {
			ggCom.getInstance().PM( "E8", offset, this.time );

			super.seek( offset );
		}
		public override function togglePause( ):void {
			ggCom.getInstance().PM( "togglePause", this.time );

			super.togglePause( );
		}
		public override function resume( ):void {
			ggCom.getInstance().PM( "E5", null );

			super.resume( );
		}
		public override function close( ):void {
			ggCom.getInstance().PM( "close", this.time );
			super.close( );
		}
	}
}

class ggClient {
	private var _userClient:Object;
	private var _flvName:String;
	private var _videoInfo:String;
	private var _ggNetStream:*;

	public function ggClient( userClient:Object, flvName:String, videoInfo:String, ggNetStream:* ):void {
		_userClient=userClient;
		_flvName=flvName;
		_videoInfo=videoInfo;
		_ggNetStream=ggNetStream;
	}

	public function asyncError( info:Object ):void {
		// Return control to program
		if (_userClient.asyncError!=null) {
			_userClient.asyncError( info );
		}
	}

	public function ioError( info:Object ):void {
		// Return control to program
		if (_userClient.ioError!=null) {
			_userClient.ioError( info );
		}
	}

	public function netStatus( info:Object ):void {
		// Return control to program
		if (_userClient.netStatus!=null) {
			_userClient.netStatus( info );
		}
	}

	public function onCuePoint( info:Object ):void {
		// Return control to program
		if (_userClient.onCuePoint!=null) {
			_userClient.onCuePoint( info );
		}
	}

	public function onImageData( info:Object ):void {
		// Return control to program
		if (_userClient.onImageData!=null) {
			_userClient.onImageData( info );
		}
	}

	public function onMetaData( info:Object ):void {
		if (info.title!=null&&info.title!=""&&_videoInfo.indexOf("<title>")==-1) {
			_videoInfo+="<title>"+info.title+"</title>";
		}

		// Catch onMetaData call, send needed info to Glance Guide  
//		ggCom.getInstance().PM( "E3", info, _flvName, _videoInfo);

		// Reset client to userClient
		_ggNetStream.client=_userClient;

		// Return control to program
		if (_userClient.onMetaData!=null) {
			_userClient.onMetaData( info );
		}
	}

	public function onPlayStatus( info:Object ):void {
		// Return control to program
		if (_userClient.onPlayStatus!=null) {
			_userClient.onPlayStatus( info );
		}
	}

	public function onTextData( info:Object ):void {
		// Return control to program
		if (_userClient.onTextData!=null) {
			_userClient.onTextData( info );
		}
	}
}